﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Gmis.Public;

namespace Gmis.Web.Manage.Village
{
    /// <summary>
    /// VillageHandler 的摘要说明
    /// </summary>
    public class VillageHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
           
            string sId = context.Request["id"];
            string action = context.Request["action"];

            //获取form数据
            Model.Village model = new Model.Village();
            model.sId = context.Request["txt_sId"];
            if (model.sId == "") model.sId = CommonFunction.GetGuid();//
            model.sName = CommonFunction.getRequestValue("txt_sName");// context.Request["txt_sName"];
            model.sShort = context.Request["txt_sShort"];
            model.nSort = CommonFunction.varToIntDef(context.Request["txt_nSort"], 1);
            model.sDescription = context.Request["txt_sContent"];
            model.sAddress = context.Request["txt_sAddress"];
            //
            if (string.IsNullOrEmpty(action))
            {
                context.Response.Write(CommonFunction.failure("无指定操作"));
            }
            else
            {
                switch (action.ToLower())
                {
                    case "add":
                        context.Response.Write(add(model));
                        break;
                    case "delete":
                        context.Response.Write(delete(sId));
                        break;
                    case "deleteall":
                        context.Response.Write(deleteAll(context.Request["ids"]));
                        break;
                    case "edit":
                        context.Response.Write(update(model));
                        break;
                }
            }
        }
        //增加一条
        private string add(Model.Village model)
        {
            BLL.Village bll = new BLL.Village();
            if (bll.Add(model))
            {
                //return CommonFunction.success("", "VillageList");
                return CommonFunction.successAndCloseCurrent("", "", "VillageList");
            }
            else
            {
                return CommonFunction.failure();
            }
        }
        //删除一条
        private string delete(string sId)
        {
            BLL.Village bll = new BLL.Village();
            if (bll.Delete(sId))
            {
                return CommonFunction.success();
            }
            else
            {
                return CommonFunction.failure();
            }
        }
        //删除全部
        private string deleteAll(string ids)
        {
            string[] strs = ids.Split(',');
            try
            {
                BLL.Village bll = new BLL.Village();
                foreach (string str in strs)
                {
                    bll.Delete(str);
                }
                return CommonFunction.success();
            }
            catch { return CommonFunction.failure(); }


        }
        //更新数据
        private string update(Model.Village model)
        {
            BLL.Village bll = new BLL.Village();
            if (bll.Update(model))
            {
                return CommonFunction.successAndCloseCurrent("", "", "VillageList");
            }
            else
            {
                return CommonFunction.failure();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }


    }
}